package com.ahdms.es.parse;

import org.bouncycastle.asn1.ASN1Sequence;

/**
 * @author qinxiang
 * @date 2021-04-12 16:25
 */
public class VersionParser {

    public static SESVersionHolder parseSES_SealVersion(Object o) {
        ASN1Sequence seq = ASN1Sequence.getInstance(o);
        SESVersion version;
        if (seq.size() == 4) {
            version = SESVersion.v4;
        } else {
            if (seq.size() != 2) {
                throw new IllegalArgumentException("未知的数据结构，无法匹配任何已知版本电子印章。");
            }

            version = SESVersion.v1;
        }

        return new SESVersionHolder(version, seq);
    }

    public static SESVersionHolder parseSES_SignatureVersion(Object o) {
        ASN1Sequence seq = ASN1Sequence.getInstance(o);
        SESVersion version;
        if (seq.size() >= 4 && seq.size() <= 5) {
            version = SESVersion.v4;
        } else {
            if (seq.size() != 2) {
                throw new IllegalArgumentException("未知的数据结构，无法匹配任何已知版本电子签章数据。");
            }

            version = SESVersion.v1;
        }

        return new SESVersionHolder(version, seq);
    }

}
